import AudioManager from "./AudioManager";
import frameworkManager from "./frameworkManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UiManager extends cc.Component {
    public static addButtonListen(
        node: cc.Node,
        callback: Function,
        target: any,
        interval: number = 300,
        eventType: string = "click",
        transition: cc.Button.Transition = cc.Button.Transition.SCALE
    ): void {
        if (node) {
            let button = node.getComponent(cc.Button);
            if (!button) {
                button = node.addComponent(cc.Button);
                button.transition = transition;
            }
            
            node.on(eventType, () => {
                AudioManager.getInstance().playMusic(eventType);
                if (callback) {
                    callback.bind(target)();
                    if (interval) {
                        button.interactable = false;
                        setTimeout(() => {
                            if (cc.isValid(button)) {
                                button.interactable = true;
                            }
                        }, interval);
                    }
                }
            }, target);
        }
    }

    public static loaderView(
        parent: cc.Node,
        prefabName: string,
        initData?: any,
        zIndex: number = 0,
        callback?: Function
    ): void {
        cc.resources.load(`prefabs/${prefabName}`, cc.Prefab, (error, prefab) => {
            if (error) {
                frameworkManager.error(error);
            } else {
                let instance:cc.Node = cc.instantiate(prefab);
                parent.addChild(instance, zIndex);
                instance.addComponent(`${prefabName}Ctrl`);
                if (callback) {
                    callback(instance);
                }
                if (initData) {
                    instance.getComponent(`${prefabName}Ctrl`).initData(initData);
                }
            }
        });
    }

    public static loaderViewDeepPath(
        parent: cc.Node,
        prefabName: string,
        subfolder: string,
        initData?: any,
        zIndex: number = 0
    ): Promise<cc.Prefab> {
        return new Promise((resolve, reject) => {
            cc.resources.load(`prefabs/${prefabName}`, cc.Prefab, (error, prefab) => {
                if (error) {
                    frameworkManager.error("loaderViewDeepPath==", error);
                    reject("未找到资源");
                } else {
                    const instance = cc.instantiate(prefab);
                    parent.addChild(instance, zIndex);
                    instance.addComponent(`${prefabName}Ctrl`);
                    if (initData) {
                        instance.getComponent(`${prefabName}Ctrl`).initData(initData);
                    }
                    resolve(prefab);
                }
            });
        });
    }

    public static loaderPrefab(prefabName: string): Promise<cc.Prefab> {
        return new Promise((resolve, reject) => {
            cc.resources.load(`prefabs/${prefabName}`, cc.Prefab, (error, prefab) => {
                if (error) {
                    frameworkManager.error("loaderPrefab==", error);
                    reject("未找到资源");
                } else {
                    resolve(prefab);
                }
            });
        });
    }

    public static async loaderPrefabInDeepPath(prefabPath: string): Promise<cc.Prefab> {
        return new Promise((resolve, reject) => {
            cc.resources.load(prefabPath, cc.Prefab, (error, prefab) => {
                if (error) {
                    frameworkManager.error("loaderPrefabInDeepPath==", error, prefabPath);
                    reject("未找到资源");
                } else {
                    resolve(prefab);
                }
            });
        });
    }

    public static loadSpriteFrame(
        target: cc.Node,
        subfolder: string,
        imageName: string,
        callback?: Function
    ): void {
        cc.resources.load(
            `imgs/${subfolder}/${imageName}`,
            cc.SpriteFrame,
            (error, spriteFrame) => {
                if (error) {
                    cc.error(error);
                } else if (target && target.getComponent(cc.Sprite)) {
                    target.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                    if (callback) {
                        callback();
                    }
                }
            }
        );
    }

    public static loadSpriteFrameInDeepPath(
        target: cc.Node,
        spriteFramePath: string
    ): Promise<cc.SpriteFrame> {
        return new Promise((resolve, reject) => {
            cc.resources.load(spriteFramePath, cc.SpriteFrame, (error, spriteFrame) => {
                if (error) {
                    frameworkManager.error("loadSpriteFrameInDeepPath==", error);
                    reject("未找到资源");
                } else {
                    if (target && target.getComponent(cc.Sprite)) {
                        target.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                    }
                    resolve(spriteFrame);
                }
            });
        });
    }

    public static loaderHead(url: string, target: cc.Node, scale: number = 100): void {
        cc.assetManager.loadRemote(url, (error, texture) => {
            if (error) {
                frameworkManager.log("头像加载失败", error);
            } else {
                target.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                target.scale = scale / target.getContentSize().width;
            }
        });
    }
}
